﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using SIO = System.IO;
using System.Reflection;

namespace remes.BLoc
{
   /// <summary>
   /// Global settings for BamlLocalizer
   /// </summary>
   /// <remarks>
   /// BLocSettings is a singelton object. Use the Instance property to access the object.
   /// </remarks>
   public class BLocSettings
   {
      /// <summary>
      /// static constructor
      /// </summary>
      static BLocSettings()
      {
         Instance = new BLocSettings();
      }

      /// <summary>
      /// Gets the Singleton Object of BLocSettings
      /// </summary>
      public static BLocSettings Instance { get; private set; }

      /// <summary>
      /// Constructor
      /// </summary>
      private BLocSettings()
      {

      }

      /// <summary>
      /// Gets the full path of the MSBuild.exe tool for the given framework version.
      /// </summary>
      /// <param name="targetFrameworkVersion">Target framework version.</param>
      /// <remarks>
      /// The targetFrameworkVersion can have a 'v' as prefix.
      /// Example: 'v3.5', '3.5', 'v4.0', '4.0', ...
      /// </remarks>
      /// <returns>Returns the full path of the MSBuild tool for thze given framework version</returns>
      public string GetMSBuildPath(string targetFrameworkVersion)
      {
         if (targetFrameworkVersion.StartsWith("v", StringComparison.OrdinalIgnoreCase))
         {
            targetFrameworkVersion = targetFrameworkVersion.Substring(1);
         }
         var rk = Registry.LocalMachine.OpenSubKey(String.Format(@"SOFTWARE\Microsoft\MSBuild\ToolsVersions\{0}", targetFrameworkVersion), false);
         try
         {
            return SIO.Path.Combine((string)rk.GetValue("MSBuildToolsPath"), "MSBuild.exe");
         }
         finally
         {
            rk.Close();
         }
      }

      /// <summary>
      /// Gets the full path of the LocBaml command line utility.
      /// </summary>
      public string LocBamlPath
      {
         get { return SIO.Path.Combine(SIO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "LocBamlForBLoc.exe"); }
      }

      /// <summary>
      /// Gets the full path of the BLoc command line utility.
      /// </summary>
      public string BLocCLUPath
      {
         get { return SIO.Path.Combine(SIO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "BLocCLU.exe"); }
      }

      /// <summary>
      /// Gets if the State of a text should be set to translated automatically when the text is modified.
      /// </summary>
      public bool AutoSetStateTranslated
      {
         get { return true; }
      }
   }
}
